package cn.study.controller;


import cn.study.entity.StudyMenu;
import cn.study.entity.StudyRole;
import cn.study.entity.StudyRoleMenu;
import cn.study.service.StudyMenuService;
import cn.study.service.StudyRoleMenuService;
import cn.study.service.StudyRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 角色表
 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@Slf4j
@RequestMapping("/study-role")
public class StudyRoleController {

    @Resource
    StudyRoleService studyRoleService;

    @Resource
    StudyMenuService studyMenuService;

    @Resource
    StudyRoleMenuService studyRoleMenuService;

    @GetMapping("/list")
    public String list(Model model,@RequestParam(value="pn",defaultValue="1") Integer pn,
                       @RequestParam(value="paginalsize",defaultValue = "5") Integer paginalsize)
    {
        PageHelper.startPage(pn, paginalsize);

        List<StudyRole> list=studyRoleService.getlist();
        PageInfo pInfo=new PageInfo(list,10);
        model.addAttribute("pInfo",pInfo);
        model.addAttribute("paginalsize",paginalsize);

        return "study_role";
    }
    @GetMapping("/add")
    public String add(Model model)
    {
        List<StudyMenu> studyMenus=studyMenuService.list();
        System.out.println(studyMenus);
        model.addAttribute("list",studyMenus);
        return "study_role_add";
    }

    @PostMapping("/roleadd")
    public String roleadd(StudyRole role,String menuroleid ){
        System.out.println("sadasdadasdasd"+menuroleid);
        boolean bl=studyRoleService.save(role);
        QueryWrapper<StudyRole> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time").last("limit 0,1 ");
        StudyRole studyRole = studyRoleService.getOne(queryWrapper);
        String[] ars=menuroleid.split(",");
        List<StudyRoleMenu> studyRoleMenus=new ArrayList<>();
       for(int i=0;i<ars.length;i++){
           StudyRoleMenu studyRoleMenu=new StudyRoleMenu();
           studyRoleMenu.setRoleId(studyRole.getId());
           studyRoleMenu.setMenuRoleId(Integer.parseInt(ars[i]));
           studyRoleMenus.add(studyRoleMenu);
       }

       boolean b2= studyRoleMenuService.saveBatch(studyRoleMenus);
        if(bl){
            log.info("添加成功");
        }else {
            log.info("添加失败");
        }
        if(b2){
            log.info("添加成功");
        }else {
            log.info("添加失败");
        }
        return "redirect:/study-role/list";
    }


    @GetMapping("del")
    public String del(Integer id)
    {
        boolean bl=studyRoleService.removeById(id);
        if(bl){
            log.info("删除成功");
        }else {
            log.info("删除失败");
        }
        return "redirect:/study-role/list";
    }
    @GetMapping("/upd")
    public String upd(Integer id,Model model)
    {
        StudyRole role=studyRoleService.getById(id);
        model.addAttribute("list",role);

        List<StudyMenu> studyMenus=studyMenuService.list();
        QueryWrapper<StudyRoleMenu> queryWrapper=new QueryWrapper();
        queryWrapper.eq("role_id",id);
        List<StudyRoleMenu> studyRoleMenus= studyRoleMenuService.list(queryWrapper);
        model.addAttribute("list3",studyRoleMenus);
        model.addAttribute("list2",studyMenus);

        return "study_role_upd";
    }

    @PostMapping("/roleupd")
    public String roleupd(StudyRole role,String menuroleid )
    {
            studyRoleService.updateById(role);


        QueryWrapper<StudyRoleMenu> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("role_id",role.getId());
        studyRoleMenuService.remove(queryWrapper1);


        System.out.println("sadadassadasdasdsadasdasda"+menuroleid);
        String[] ars=menuroleid.split(",");
        List<StudyRoleMenu> studyRoleMenus=new ArrayList<>();
        for(int i=0;i<ars.length;i++){
            StudyRoleMenu studyRoleMenu=new StudyRoleMenu();
            studyRoleMenu.setRoleId(role.getId());
            studyRoleMenu.setMenuRoleId(Integer.parseInt(ars[i]));
            studyRoleMenus.add(studyRoleMenu);
        }

        boolean b2= studyRoleMenuService.saveBatch(studyRoleMenus);
        if(b2){
            log.info("添加成功");
        }else {
            log.info("添加失败");
        }
        return "redirect:/study-role/list";
    }
}

